<?php

/**
 * cms actions.
 *
 * @package    Amman to London
 * @subpackage cms
 * @author     Adel Abu-Saif <adeljas@msn.com>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class cmsActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeShow(sfWebRequest $request) {
        $this->obj = $this->getRoute()->getObject();
    }

    public function executeShowBlog(sfWebRequest $request) {
        if ($request->hasParameter('id'))
            $this->obj = BlogTable::getInstance()->find($request->getParameter('id'));
        else
            $this->obj = $this->getRoute()->getObject();
    }

    public function executePostComment(sfWebRequest $request) {
        $user = $this->getUser()->getGuardUser();
        $comment = $request->getParameter('comment');
        $username = $request->getParameter('username');
        $item_id = $request->getParameter('item_id');
        $parent_id = $request->getParameter('parent_id', false);

        Doctrine::getTable('Comment')->postComment($username, $item_id, $comment, $parent_id);

        sfContext::getInstance()->getConfiguration()->loadHelpers(array('I18N'));

        if ($request->isXmlHttpRequest())
            exit;
        else
            return $this->renderText('<div style="font-size:11px;color:#000;font-family:tahoma;text-align:center;padding:2px;background-color:#77FF77">' . __('Thank you for posting, your comment will appear after being approved by us.') . '</div>');
    }

    public function executeNews_list($request) {
        $this->pager = new sfDoctrinePager('Content', 8);
        $q = Doctrine_Query::create()->orderBy('created_at DESC')->from('Content c');
        $q->addWhere('c.is_player_page = 0');
        $q->leftJoin('c.Translation t');
        $q->addWhere('t.lang = ?', $this->getUser()->getCulture());
        $q->addWhere('t.title <> ""');

        if ($request->hasParameter('c'))
            $q->addWhere('c.category_id = ?', $request->getParameter('c'));

        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->setQuery($q);
        $this->pager->init();
        $this->title = 'Latest News';
        $this->is_content = true;
    }

    public function executeBlog_list($request) {
        $q = Doctrine_Query::create()->orderBy('created_at DESC')->from('Blog c');
        $q->leftJoin('c.Translation t');
        $q->addWhere('t.lang = ?', $this->getUser()->getCulture());
        $q->addWhere('t.title <> ""');
        $this->pager = new sfDoctrinePager('Blog', 8);
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->setQuery($q);
        $this->pager->init();
        $this->title = 'Latest Editor\'s Pick';
        $this->setTemplate('news_list');
        $this->is_content = false;
    }

    public function executeSearch($request) {
        $search = trim($request->getParameter('search_query'));
        if ($search == '')
            $this->redirect($request->getReferer());
        // simple search method as there will be no much content on this website
        $uc_culture = ucwords($this->getUser()->getCulture());
        $q = Doctrine_Query::create()->orderBy('updated_at DESC')->from('Search' . $uc_culture);
        $q->where('title LIKE ?', '%' . $search . '%');
        $q->orWhere('brief LIKE ?', '%' . $search . '%');
        $q->orWhere('body LIKE ?', '%' . $search . '%');
        $this->pager = new sfDoctrinePager('Search' . $uc_culture, 8);
        $this->pager->setPage($request->getParameter('page', 1));
        $this->pager->setQuery($q);
        $this->pager->init();
    }

    public function executeShow_university_news($request) {
        $id = $request->getParameter('id', false);
        if ($id === false)
            exit;

        $this->obj = UniversityNewsTable::getInstance()->find($id);
    }

    public function executeSuccess_messages($request) {
        $this->message = $request->getParameter('message', false);
         $this->form = new sfGuardFormSignin();
        $this->setLayout("login");
       
    }
    public function executeMessages($request) {
        $this->message = $request->getParameter('message', false);
         $this->form = new sfGuardFormSignin();
        $this->setLayout("login");
       
    }


}
